package com.buka.dao;

import com.buka.entity.Movie;
import com.buka.entity.MovieType;
import com.buka.util.JDBCUtil;

import java.util.List;

public class MovieDao {

    public int add(Movie movie){
        String sql ="insert into movie (name,yanyuan,type,imageUrl,desc,publishTime) " +
                " values (?,?,?,?,?,?) ";
        return JDBCUtil.executeUpdateByArr(sql,movie.getName(),movie.getYanyuan(),movie.getType(),movie.getImageUrl(),movie.getDesc(),movie.getPublishTime());
    }

    public int updateById(Movie movie){
        String sql ="update movie set name=?,yanyuan=?,type=?,imageUrl=?,desc=?,publishTime=?" +
                " where id = ?";
        return JDBCUtil.executeUpdateByArr(sql,movie.getName(),movie.getYanyuan(),movie.getType(),movie.getImageUrl(),movie.getDesc(),movie.getPublishTime(),movie.getId());
    }

    //删除
    public int deleteById(Integer id){
        String sql ="delete from movie where id = ?";
        return JDBCUtil.executeUpdateByArr(sql,id);
    }

    //查询全部记录
    public List<Movie> list(){
        String sql ="select * from movie";
        return JDBCUtil.executeQuery(sql,Movie.class);
    }

    /**
     * 按类型查询数据，返回3条记录
     * @param type
     * @return
     */
    public List<Movie> selectByType(Integer type){
        String sql ="select * from movie where type = ? order by publishTime desc limit 3";
//        String sql ="select * from movie where type = ? limit 0,3";
        return JDBCUtil.executeQuery(sql,Movie.class,type);
    }

    /**
     * 查询全部电影类型
     */
    public List<MovieType> selectAllType(){
        String sql ="select * from movie_type";
        return JDBCUtil.executeQuery(sql,MovieType.class);
    }


}
